package com.sky.service.impl;


import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.properties.JwtProperties;
import com.sky.service.ShoppingCartService;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 购物车添加
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
      ShoppingCart shoppingCart=new ShoppingCart();
      BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

      //判断购物车里面包含当前商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShopByShoppingCart(shoppingCart);

        if(shoppingCartList!=null&&shoppingCartList.size()>0){
            //如果存在，就数量加1
            shoppingCart=shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //不存在，数量默认是1
            shoppingCart.setNumber(1);

            //判断添加到购物车里面是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if(dishId!=null){
                //添加到购物车的是菜品
                Dish dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);


        }




    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.showShoppingCart(0L);

        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteShoppingCart() {
        shoppingCartMapper.deleteShoppingCart(0L);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);


        //查询当前商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShopByShoppingCart(shoppingCart);
        shoppingCart = shoppingCartList.get(0);

        //查询当前商品的数量，如果数量是1就删除，如果数量不是1减1
        Integer number = shoppingCart.getNumber();
        if(number!=null&&number>1){
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }


    }


}
